home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9076 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.9 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Q:  gets, getch, and stdin
  5. Date: Wed, 06 Mar 96 22:28:27 GMT
  6. Organization: none
  7. Message-ID: <826151307snz@genesis.demon.co.uk>
  8. References: <DnoC9w.Eq9@info.uucp>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <DnoC9w.Eq9@info.uucp> rede4740@mach1.wlu.ca "Chris Redekop u" writes:
  15.  
  16. >
  17. >Hi.  I'm trying to teach myself C with the help of 'Teach Yourself C In 
  18. >21 Days'.  Everything was going fine until day 14, 'Working with the 
  19. >Screen, Printer, and Keyboard'.
  20.  
  21. Are you aware that Screen, Printer, and Keyboard are not things that the
  22. C language supports. All it knows about are files and 'streams' which you
  23. can open to files to read and write data. stdin, stdout and stderr are
  24. examples of streams that are opened for you when the program starts.
  25. Your particular system can contrive to attach Screen etc. to streams
  26. effectively simulating files but knowing this may help you understand how the
  27. I/O works.
  28.  
  29. > On page 319 (in case anyone reading this 
  30. >has the book), it talked about how using scanf can leave unwanted 
  31. >characters in stdin.  It offered this function as a way to clear stdin:
  32.  
  33. The facilities that scanf provides simply doesn't make it particularly
  34. suitable for line based input - it doesn't treat '\n' specially, just like
  35. any other white-space.
  36.  
  37. >void clear_kb(void)
  38. >{
  39. >char junk[80];
  40. >gets(junk);
  41. >}
  42.  
  43. This is evil - gets() is historical baggage that you should never use.
  44. The problem is that you can't tell it the size of the buffer you passed it
  45. so if it encounters a line that is longer than the buffer it will overwrite
  46. the end of it and corrupt anything that happens to be there.
  47.  
  48. >
  49. >I thought to myself, 'That might work, but it uses a whole array to do 
  50. >it.  Instead of clearing stdin by reading a whole string, I should be 
  51. >able to clear it one chartacter at a time, by using getch().  Then I 
  52. >would only need one local variable of type char (or int).'  So I tried this:
  53.  
  54. getch() is not a standard C function so its behaviour is not well defined
  55. and could vary from implementation to implementation. That happens in
  56. practice: under Unix getch() is part of the Curses library and works
  57. differently in relation to other functions that does the version you
  58. typically find under DOS. If your book didn't make it clear that getch()
  59. is non-standard that's the 2nd (or is that the 3rd?) black mark.
  60.  
  61. >void clear_kb(void)
  62. >{
  63. >char ch;
  64. >
  65. >while ((ch = getch()) != '\r')
  66. >        ;
  67. >}
  68. >
  69. >This didn't work, so after I tried a few different things, I found that 
  70. >this works:
  71. >
  72. >void clear_kb(void)
  73. >{
  74. >char ch;
  75. >
  76. >while ((ch = getchar()) != '\n')
  77. >        ;
  78. >}
  79.  
  80. getchar() is a standard C library function however it returns int. There
  81. is a good reason for this because as well as any valid character (which
  82. is returned as a positive value) it can return EOF (which is a negative
  83. value) which is distinct from any valid character. It is important to test
  84. for EOF since you don't necessarily know where your input is coming from
  85. (e.g. stdin may have been redirected to a file by whatever ran your
  86. program).
  87.  
  88. >This is what confuses me.  I can't understand why the third function 
  89. >works, and not the second.  As well, the third function does not output 
  90. >the cleared characters to the screen, even though getchar provides input 
  91. >with echo.
  92.  
  93. I couldn't say what getch() does because it is not well defined.
  94.  
  95. getchar() is not defined to produce any sort of echo. However it, as well
  96. as any other standard function reading characters from an input stream,
  97. must get characters from the OS. In the case of stdin this is usually
  98. connected to the keyboard when the program starts. The OS will typically
  99. arrange to perform some rudimentary command line editing before making
  100. the complete line available to the program, e.g. when you press <return>.
  101. That line editing may well involve echoing characters to the screen but
  102. all of this is performed before the line is passed to the C program.
  103. The C library in general and getchar() in particular performs no echoing.
  104.  
  105. The correct way to approach line based input is to read the line as a whole
  106. into a buffer using fgets(). fgets() allows you to specify the size of
  107. the buffer you pass it so doesn't suffer the same defect as gets(). Also
  108. remember it leaves the trailing '\n' at the end of the string. Once you
  109. have the line in the buffer you can pick it apart with your own code or
  110. a range of standard library functions e.g. atoi(), strtol(), sscanf(),
  111. strtok() etc.
  112.  
  113. I suggest you look through the FAQ which provides much information
  114. in these areas. You can ftp it from rtfm.mit.edu under
  115. /pub/usenet/comp.lang.c.
  116.  
  117. -- 
  118. -----------------------------------------
  119. Lawrence Kirby | fred@genesis.demon.co.uk
  120. Wilts, England | 70734.126@compuserve.com
  121. -----------------------------------------
  122.